package code.structure.manager.door
{
	import code.structure.manager.bitmap.YDisplayConvert;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	/**
	 *门的素材管理器 
	 * @author Yuri
	 * 
	 */
	public class DoorMaterialManager
	{
		/**
		 *门素材字典 
		 */
		private var _doorDic:Dictionary;
		private var _callBackFunDic:Dictionary;
		
		private static var _instance:DoorMaterialManager
		public function DoorMaterialManager()
		{
			if(_instance){ 
				throw new Error("实例化单例类出错-DoorMaterialManager");
			}else{
				_doorDic = new Dictionary();
				_callBackFunDic = new Dictionary();
			}
		}
		
		public static function getInstance():DoorMaterialManager{
			if(!_instance) _instance = new DoorMaterialManager();
			return _instance;
		}
		
		/**
		 *获取门信息 
		 * @param key
		 * @param c
		 * @param callBackFun
		 * 
		 */
		public function getDoorMaterialData(key:String,c:Class,callBackFun:Function):void{
			if(callBackFun==null) return;
			if(_doorDic[key]){
				callBackFun(_doorDic[key]);
			}else{
				if(!_callBackFunDic[key])_callBackFunDic[key] = [];
				if(_callBackFunDic[key].indexOf(callBackFun) == -1)
					_callBackFunDic[key].push(callBackFun);
				analyData(key,c);
			}
		}
		
		private function callBack(key:String):void{
			if(_callBackFunDic[key] && _doorDic[key]){
				var arr:Array = _callBackFunDic[key] as Array;
				var count:int = arr.length;
				var fun:Function;
				for(var i:int;i<count;i++){
					fun = arr[i];
					fun(_doorDic[key]);
				}
				delete _callBackFunDic[key];
			}
		}
		
		/**
		 *解析 
		 * @param key
		 * @param c
		 * 
		 */
		private function analyData(key:String,c:Class):void{
			var sp:MovieClip = new c() as MovieClip;
			var t:DoorMaterialData = new DoorMaterialData();
			t.doorBitmapList = YDisplayConvert.drawMoveClip(sp)[0] as Array;
			while(sp.numChildren>0){
				var temp:DisplayObject = sp.getChildAt(0) as DisplayObject;
				t.addBitMapData(YDisplayConvert.drawBitMapData(temp),temp.x,temp.y);
				sp.removeChildAt(0);
			}
			sp = null;
			_doorDic[key] = t;
			callBack(key);
		}
	}
}